﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Angel.Daos.IManager;
using Angel.Entities;

namespace Angel.Daos.Manager
{
    public class GroupManager: IGroupManager
    {
        AngelDataContext ctx = new AngelDataContext();

        public IEnumerable<Group> GetAll()
        {
            return ctx.Groups.ToList();
        }
        public Group GetByID(int id)
        {
            return ctx.Groups.SingleOrDefault(g => g.GroupID == id);
        }

      

        public void Add(Group group)
        {     
            ctx.Groups.InsertOnSubmit(group);
            ctx.SubmitChanges();
        }

        public bool CheckGroupName(string name)
        {
            bool check = false;
            List<Group> lists = GetAll().ToList();
            foreach (Group group in lists)
            {
                string s = group.GroupName.Trim();
                if (s.Equals(name))
                {
                    check = true;
                    break;
                }
            }
            return check;
        }

        public void Delete(Group group)
        {
            ctx.Group_Users.DeleteAllOnSubmit(group.Group_Users);
            ctx.Groups.DeleteOnSubmit(group);
            ctx.SubmitChanges();
        }

        public void Delete(int id)
        {
            throw new NotImplementedException();
        }

        /// Author: Phan Duc Chien
        public IEnumerable<Group> Search(String groupName, int start, int max)
        {
            //Return list of groups to be searched.
            IEnumerable<Group> listGroup = (from row in ctx.Groups
                                            where row.GroupName.ToString().Contains(groupName)
                                            select row).OrderBy(m => m.GroupName);

            //IEnumerable<Group> result = listGroup.Skip(start).Take(max);
            return listGroup;
        }        
        public IEnumerable<Group_User> GetData(int id)
        {
            IEnumerable<Group_User> result = from item in ctx.Group_Users
                         where item.GroupID==id
                         select item;
            System.Diagnostics.Debug.Write(result);
            return result;
        }


        public int countUser(Group group)
        {
            return ctx.Group_Users.Count(m => m.GroupID == group.GroupID);
        }
    }
}
